<?php

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');

class SocietyController extends Controller
{

    public function init()
    {
        $this->authentication = $this->authentication();
        if(!$this->authentication)
        {
            $this->redirect('/');
            exit;
        }
        $this->society = new SocietyModel();
        $this->getView()->assign("user", $this->authentication['data']);
        $this->group = new GroupModel();
        $group = $this->group->listsAction($this->authentication['data']);
        $this->getView()->assign("group", $group);
    }

    public function indexAction()
    {
        $page=$this->getRequest()->get("page");
        $myPosts = $this->society->myPostsAction($this->authentication['data']);
        $societyLists = $this->society->listsAction($this->authentication['data']['fid'],$page);
        $this->getView()->assign("myPosts", $myPosts);
        $this->getView()->assign("societyLists", $societyLists);
        $this->getView()->assign("title", '论坛');
    }

    public function detailAction()
    {
        $sid=$this->getRequest()->get("sid");
        $detailed = $this->society->detailedAction($this->authentication['data']['fid'],$sid);
        $this->getView()->assign("detailed", $detailed);
        $this->getView()->assign("title", '详细');
    }

    public function commentAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'sid' => 'require|number|egt:1|exist:society,id',
                'page' => 'require|number|egt:1',
            ]);
            $validate->message([
                'sid.require' => '帖子ID不能为空',
                'sid.number' => '帖子ID类型错误',
                'sid.egt' => '帖子ID不在范围内',
                'sid.exist' => '帖子不存在',
                'page.require' => '页码不能为空',
                'page.number' => '页码类型错误',
                'page.egt' => '起始页过小',
            ]);
            if ($validate->check($content)) {
                $code = 200;
                $data = $this->society->commentAction($this->authentication['data']['fid'],$content);
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function commentPostAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $content['content'] = $this->filterEmoji($content['content']);
            $validate = new Validate([
                'sid' => 'require|number|egt:1',
                'content' => 'require|min:2|max:512',
            ]);
            $validate->message([
                'sid.require' => '帖子ID不能为空',
                'sid.number' => '帖子ID只能是数字',
                'sid.egt' => '帖子ID不在范围内',
                'content.require' => '内容不能为空',
                'content.min' => '内容不能小于2个字符',
                'content.max' => '内容不能大于512个字符',
            ]);
            if ($validate->check($content)) {
                $code = 200;
                $data = $this->society->commentPostAction($this->authentication['data'],$content);
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function sendAction()
    {
        $myPosts = $this->society->myPostsAction($this->authentication['data']);
        $this->getView()->assign("imgPath", HTTP_WEB.VERSION_LIVESTAR.'/Default/');
        $this->getView()->assign("myPosts", $myPosts);
        $this->getView()->assign("title", '发布');
    }

    public function sendPostAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $content['title'] = $this->filterEmoji($content['title']);
            $content['content'] = $this->filterEmoji($content['content']);
            $validate = new Validate([
                'title' => 'require|chsSymbol|min:2|max:32',
                'content' => 'require|min:2|max:512',
            ]);
            $validate->message([
                'title.require' => '标题不能为空',
                'title.chsSymbol' => '标题只能是只允许汉字、字母、数字和中文标点符号',
                'title.min' => '标题不能小于2个字符',
                'title.max' => '标题不能大于32个字符',
                'content.require' => '内容不能为空',
                'content.min' => '内容不能小于2个字符',
                'content.max' => '内容不能大于512个字符',
            ]);
            if ($validate->check($content)) {
                $code = 200;
                $data = $this->society->sendPostAction($this->authentication['data'],$content);
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function postsAction()
    {
        $page=$this->getRequest()->get("page");
        $posts = $this->society->postsAction($this->authentication['data'],$page);
        $this->getView()->assign("posts", $posts);
        $this->getView()->assign("title", '我的');
    }
}
